﻿using System;
using System.Configuration;
using eBlog.Cache;

namespace eBlog.Common.Files
{
	/// <summary>
	/// web.config操作类
	/// </summary>
	public sealed class ConfigHelper
	{
		/// <summary>
		/// 得到AppSettings中的配置字符串信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static string GetConfigString(string key)
		{
            string CacheKey = "AppSettings-" + key;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.AppSettings[key]; 
                    if (objModel != null)
                    {                        
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                    }
                }
                catch
                { }
            }
            return objModel.ToString();
		}

		/// <summary>
		/// 得到AppSettings中的配置Bool信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static bool GetConfigBool(string key)
		{
			bool result = false;
			string cfgVal = GetConfigString(key);
			if(!string.IsNullOrEmpty(cfgVal))
			{
				try
				{
					result = bool.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}
			return result;
		}
		/// <summary>
		/// 得到AppSettings中的配置Decimal信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static decimal GetConfigDecimal(string key)
		{
			decimal result = 0;
			string cfgVal = GetConfigString(key);
			if(!string.IsNullOrEmpty(cfgVal))
			{
				try
				{
					result = decimal.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}

			return result;
		}
		/// <summary>
		/// 得到AppSettings中的配置int信息
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static int GetConfigInt(string key)
		{
			int result = 0;
			string cfgVal = GetConfigString(key);
			if(!string.IsNullOrEmpty(cfgVal))
			{
				try
				{
					result = int.Parse(cfgVal);
				}
				catch(FormatException)
				{
					// Ignore format exceptions.
				}
			}

			return result;
		}


        //private static string _siteprefix;

        ///// <summary>
        ///// Cache、Session、Cookies 等应用程序变量名前缀
        ///// </summary>
        //public static string SitePrefix
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(_siteprefix))
        //        {
        //            _siteprefix = GetValue("loachs_siteprefix");
        //        }
        //        return _siteprefix;
        //    }
        //}

        private static string _sitepath;

        /// <summary>
        /// 程序相对根路径
        /// </summary>
        public static string SitePath
        {
            get
            {
                if (string.IsNullOrEmpty(_sitepath))
                {
                    _sitepath = GetValue("eblog_sitepath");

                    if (!_sitepath.EndsWith("/"))
                    {
                        _sitepath += "/";
                    }
                }

                return _sitepath;
            }
        }

        private static string _siteurl;

        /// <summary>
        /// 程序Url
        /// 未考虑https://;存在多个域名指向时，有BUG，因为静态变量已存在，如www.eblog.com,eblog.com
        /// </summary>
        public static string SiteUrl
        {
            get
            {
                if (string.IsNullOrEmpty(_siteurl))
                {
                    _siteurl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + SitePath;
                    if (System.Web.HttpContext.Current.Request.Url.Port != 80)
                    {
                        _siteurl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port + SitePath;
                    }
                }
                return _siteurl;
            }
        }

        /// <summary>
        /// Site Key 用于后台登录
        /// </summary>
	    public static string SiteKey
	    {
            get { return GetValue("SiteKey"); }

	    }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key];

        }
	}
}
